﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


public static class ExtentionsIEnumerableClass
{
    public static decimal SumElements<T>(this IEnumerable<T> collection)
    {
        decimal sum = 0;
        foreach (var item in collection)
        {
            sum += Convert.ToDecimal(item);
        }
        return sum;
    }

    public static T MinElement<T>(this IEnumerable<T> collection)
        where T : IComparable
    {
        if (!collection.GetEnumerator().MoveNext())
        {
            throw new NullReferenceException("Empty collection!");
        }
        T result = collection.First<T>();
        foreach (T item in collection)
        {
            if (item.CompareTo(result) == -1)
            {
                result = item;
            }
        }
        return result;
    }

    public static T MaxElement<T>(this IEnumerable<T> collection)
        where T : IComparable
    {
        if (!collection.GetEnumerator().MoveNext())
        {
            throw new NullReferenceException("Empty collection!");
        }
        T result = collection.First<T>();
        foreach (T item in collection)
        {
            if (item.CompareTo(result) == 1)
            {
                result = item;
            }
        }
        return result;
    }

    public static decimal AverageOfCollection<T>(this IEnumerable<T> collection)
    {
        decimal result = 0;
        int count = 0;      
        foreach (var item in collection)
        {
            result += Convert.ToDecimal(item);
            count++;
        }
        if (count == 0)
        {
            throw new NullReferenceException("Empty collection");
        }
        return result / count;
    }
}

